package cn.exrick.xboot.modules.Information.controller;

import cn.exrick.xboot.common.utils.PageUtil;
import cn.exrick.xboot.common.utils.ResultUtil;
import cn.exrick.xboot.common.utils.SecurityUtil;
import cn.exrick.xboot.common.vo.PageVo;
import cn.exrick.xboot.common.vo.Result;
import cn.exrick.xboot.modules.Information.entity.Cross;
import cn.exrick.xboot.modules.Information.entity.CrossCheck;
import cn.exrick.xboot.modules.Information.entity.River;
import cn.exrick.xboot.modules.Information.service.ICrossCheckService;
import cn.exrick.xboot.modules.Information.service.ICrossService;
import cn.exrick.xboot.modules.Information.service.IriverService;
import cn.exrick.xboot.modules.base.entity.Department;
import cn.exrick.xboot.modules.base.entity.User;
import cn.exrick.xboot.modules.base.service.DepartmentService;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author Lyg
 */
@Slf4j
@RestController
@Api(description = "断面信息管理接口")
@RequestMapping("/xboot/cross")
@Transactional
public class CrossController {
    @Autowired
    private DepartmentService departmentService;
    @Autowired
    private ICrossService iCrossService;
    @Autowired
    private IriverService iriverService;
    @Autowired
    private ICrossCheckService icrossCheckService;
    @Autowired
    private SecurityUtil securityUtil;
    @RequestMapping(value = "/get/{id}", method = RequestMethod.GET)
    @ApiOperation(value = "通过id获取")
    public Result<Cross> get(@PathVariable Long id){

        Cross cross = iCrossService.getById(id);
        return new ResultUtil<Cross>().setData(cross);
    }

    @RequestMapping(value = "/getAll", method = RequestMethod.GET)
    @ApiOperation(value = "获取全部数据")
    public Result<List<Cross>> getAll(){

        List<Cross> list = iCrossService.list();
        return new ResultUtil<List<Cross>>().setData(list);
    }
    @RequestMapping(value = "/sectionnum", method = RequestMethod.GET)
    @ApiOperation(value = "查询断面编号存在与否")
    public Result<Integer> sectionnum(@RequestParam String num){
        QueryWrapper queren = new QueryWrapper();
        queren.eq("cross_num",num);
        List<Cross> list = iCrossService.list(queren);
        return new ResultUtil<Integer>().setData(list.size());
    }

    @RequestMapping(value = "/getAllByUserId", method = RequestMethod.GET)
    @ApiOperation(value = "通过登陆人id获取断面数据")
    public Result<List<Cross>> getAllByUserId(@RequestParam String data,String model1){
        User u = securityUtil.getCurrUser();
        Department department = departmentService.get(u.getDepartmentId());
        List<Cross> list = new ArrayList<>();
        List<Cross> clist = iCrossService.crossList("");
        //按时间查询考核表，如果时间为空则查询当前日期
        List<CrossCheck> crossCheck= icrossCheckService.queryAllByDate(data,"");
        for (Cross cross:clist) {
            String s ="2";
            for (CrossCheck cc: crossCheck) {
                if(cc.getWithDepartment().equals(cross.getWithDepartment()) && "1".equals(cc.getSta()) ){
                    s=cc.getSta();
                }
            }
            cross.setSta(s);
            if(StringUtils.isNotEmpty(model1)){
                if(s.equals(model1)){
                    list.add(cross);
                }
            }else{
                list.add(cross);
            }


        }
//        if(("0").equals(department.getParentId())){
//             list = iCrossService.list();
//        }else{
//            QueryWrapper queren = new QueryWrapper();
//            queren.eq("with_department",u.getDepartmentId());
//            list = iCrossService.crossList(queren);
//        }

        return new ResultUtil<List<Cross>>().setData(list);
    }

    @RequestMapping(value = "/getByPage", method = RequestMethod.GET)
    @ApiOperation(value = "分页获取")
    public Result<IPage<Cross>> getByPage(@ModelAttribute PageVo page, @RequestParam Map map){
        //查询断面位置
        QueryWrapper queren1 = new QueryWrapper();
        if( map.containsKey("crossPlace")){
            queren1.like("cross_place", map.get("crossPlace"));
             }
        //查询所属河流
        if( map.containsKey("riverName")){
            queren1.like("river_id", map.get("riverName"));
        }

        IPage<Cross> data = iCrossService.page(PageUtil.initMpPage(page),queren1);
        for (Cross cross :data.getRecords()) {

            //行政区域赋值
            if(StrUtil.isNotBlank(cross.getWithDepartment())){
                Department department = departmentService.get(cross.getWithDepartment());
                cross.setWithDepartmentName(department.getTitle());
            }
            //所属河流名称赋值
            if(StrUtil.isNotBlank(cross.getRiverId())){
                QueryWrapper queren = new QueryWrapper();
                queren.eq("id",cross.getRiverId());
                River river = iriverService.getById(cross.getRiverId());//.get(cross.getWithDepartment());
                cross.setRiverName(river.getRiverName());
            }

        }
        return new ResultUtil<IPage<Cross>>().setData(data);
    }

    @RequestMapping(value = "/insertOrUpdate", method = RequestMethod.POST)
    @ApiOperation(value = "编辑或更新数据")
    public Result<Cross> saveOrUpdate(@ModelAttribute Cross cross){

        if(iCrossService.saveOrUpdate(cross)){
            return new ResultUtil<Cross>().setData(cross);
        }
        return new ResultUtil<Cross>().setErrorMsg("操作失败");
    }

    @RequestMapping(value = "/delByIds/{ids}", method = RequestMethod.DELETE)
    @ApiOperation(value = "批量通过id删除")
    public Result<Object> delAllByIds(@PathVariable Long[] ids){

        for(Long id : ids){
            iCrossService.removeById(id);
        }
        return new ResultUtil<Object>().setSuccessMsg("批量通过id删除数据成功");
    }
    @RequestMapping(value = "/getRiverListByRiverId/{id}", method = RequestMethod.GET)
    @ApiOperation(value = "通过河流id查询断面河流信息")
    public Result<List<Cross>> getRiverListByRiverId(@PathVariable String id){
        QueryWrapper queren = new QueryWrapper();
        queren.eq("river_id",id);
        List<Cross> ss = iCrossService.list(queren);
        River r=  iriverService.getById(id);
        ss.forEach(item->{
            item.setRiverName(r.getRiverName());
        });
        return new ResultUtil<List<Cross>>().setData(ss);
    }
}
